*************************************************************************************
************** Project: Robots and Household Financial Behavior *********************
************************  Gomes, Jansson and Karabulut ******************************
***************************** BASELINE REGRESSIONS **********************************
**********************  MAIN TEXT + ONLINE APPENDIX *********************************
*****************************  IV REGRESSIONS ***************************************
*************************************************************************************
****************************** LAST MODIFIED ****************************************
******************************** 2023-06-07 *****************************************

** Stata Settings ***
cap clear all
macro drop _all
clear
set maxvar 10000
set more off
**Define Paths**
global data "DEFINE THE PATH HERE"
global data_final "DEFINE THE PATH HERE"
global projf "DEFINE THE PATH HERE"
global pathout "DEFINE THE PATH HERE"
global logfiles "DEFINE THE PATH HERE"
**Saving a log file**
cap log close
log using "$logfiles/Table_regressions_construct_v1.log", replace

********************************************************************************
*** Uploading the Datasets ***
*(1) "$data_final/edu_arrivalf.csv"
*(2) "$data/incproc2304231993.csv"
*(3) "$data/commuting_dataf1999.csv"

*(4) "$data/robot_data_all_1999.dta"
*(5) "$projf/data/Linda_panel_robot230423.dta" 
*(6) "$projf/data/linda_ret_saving_230423.dta"
********************************************************************************

********************************************************************************
**(1) Upload and save the detailed education data for earlier years**
* from the code 'Part1a_Lindadata.sas' *
insheet using "$data_final/edu_arrivalf.csv", comma clear
gen edumajor_3_93_temp = substr(bsuninr1993,1,3)
gen edumajor_3_93 = real(edumajor_3_93_temp)
keep id edumajor_3_93 
sort id
save "$projf\data\edu_data.dta", replace

********************************************************************************
**(2) Upload and save 1993 characteristics data**
insheet using "$data_final/incproc2304231993.csv", comma clear
scalar theta=1  
gen linc93=log(theta*(inchh)+sqrt((theta*(inchh))^2+1))/theta
gen child18=0
replace child18=1 if age<18
bys idhh: egen hh_children93 = sum(child18)
rename married married93
rename hh_size hh_size93
keep id linc93 married93 hh_size93 hh_children93
sort id 
save "$projf\data\characteristics_1993.dta", replace

********************************************************************************
**(3) Upload and save commuter data**
* from the code 'Part1a_Lindadata.sas' *
insheet using "$data/commuting_dataf1999.csv", comma clear 
save "$projf/data/commuting_dataf1999.dta", replace

********************************************************************************
**(4) Upload the robot data with 1999 as base year**
use "$data/robot_data_all_1999.dta", clear
keep year SNI2_s d_rdensity_99_07_swe d_rdensity_99_07_eu_med rdensity_swe
rename d_rdensity_99_07_swe d_rdensity_99_07_swe99
rename d_rdensity_99_07_eu_med d_rdensity_99_07_eu_med99
rename rdensity_swe rdensity_swe99
save "$data/robot_data_all_1999_v2.dta", replace

********************************************************************************

********************************************************************************
**Construct the working Sample for Panel Analysis**
********************************************************************************
**(5) Construct the analysis dataset**
use "$projf/data/Linda_panel_robot230423.dta", clear 
count
**Keep the Observations from 1999**
keep if year==1999
count
**Keep those households who are exposed to robots at work*
keep if robot_ind==1
count
********************************************************************************
**(6) Merge the Data with the Cumulative Return Data by Household ID**
sort idhh
drop _merge 
merge m:1 idhh using "$projf/data/linda_ret_saving_230423.dta"
drop if _merge==2 /*Note: Drop those households that are not in our final robot sample*/
count
********************************************************************************
**Merge with commuting data**
sort id
cap drop _merge 
merge 1:1 id using "$projf/data/commuting_dataf1999.dta"
tab _merge
keep if _merge==3 
drop _merge
count
********************************************************************************
**Merge the LINDA data with the Robot Data using 1999 as base year**
sort SNI2_s year
merge m:1 SNI2_s year using "$data/robot_data_all_1999_v2.dta" 
count
**Tabulate the data**
tab _merge
*Drop obs in case there is no match of robot data with the LINDA*
drop if _merge==2 
drop _merge
********************************************************************************
**Merge the LINDA data with the Educational Transferability Scores**
sort college edumajor_3_99
merge m:1 college edumajor_3_99 using "$projf\data\transferability_index_edu3_230423.dta" 
drop if _merge==2
count
drop _merge
********************************************************************************
**Generate a dummy for individuals with low transferability of skills**
gen loghhi = log(HHI)
sum HHI loghhi hhi_id, d
bys SNI2_s: su hhi_id,d
bys SNI2_s: egen med_hhi_id=median(hhi_id)
bys SNI2_s: su HHI,d
bys SNI2_s: egen med_HHI=median(HHI)
*Use the 50th percentile (within the Industry)
gen low_transfer=1 if (hhi_id>=med_hhi_id) 
replace low_transfer=0 if (hhi_id<med_hhi_id)
**Descriptive stats**
tab low_transfer
**Descriptive stats by industry**
bys SNI2_s: tab low_transfer
**Define the interacting term**
gen ind_low_transfer=(low_transfer*d_rdensity_99_07_swe)
**Define cells for cluster**
egen ind_edu = group(SNI2_s low_transfer)
**Generate Industry Dummies based on SNI2_S
tab SNI2_s, gen(ind_fe)
********************************************************************************
**Define an Age-squared Variable**
gen age_sq=(age*age)
*Indicator for Low-Level vs. High-Level of Education (high school and less vs. college)
gen low_edu=1 if (hschool==1|nohschool==1)
replace low_edu=0 if college==1
** Income Growth between 1995-1998 ***
gen ln_inc_95=ln(inchh_earned1995)
gen ln_inc_96=ln(inchh_earned1996)
gen ln_inc_97=ln(inchh_earned1997)
gen ln_inc_98=ln(inchh_earned1998)
gen dln_earnings_95_98=(ln_inc_98-ln_inc_95)
sum dln_earnings_95_98 ln_inc_95-ln_inc_98
replace dln_earnings_95_98=(ln_inc_97-ln_inc_95) if mi(dln_earnings_95_98)==1
replace dln_earnings_95_98=(ln_inc_96-ln_inc_95) if mi(dln_earnings_95_98)==1
sum dln_earnings_95_98, d
drop ln_inc_*
********************************************************************************
**Scale the import exposure variables**
gen d_import_99_07_adj = d_import_99_07/10000
gen med_EU_exp_adj = med_EU_exp/10000
********************************************************************************
**Change base level for muni1999 to Stockholm 0180**
fvset base 180 muni1999
********************************************************************************
**Save**
count
save "$projf/data/Main_analysis_data_v1.dta", replace
********************************************************************************

********************************************************************************
**Construct the working Sample for Panel Analysis (Full Sample)**
********************************************************************************
use "$projf/data/Linda_panel_robot230423.dta", clear
**Keep the Observations from 1999**
keep if year==1999
**Keep all households with industry code*
keep if mi(sni)==0
replace d_rdensity_99_07_swe = 0 if robot_ind==0
replace d_rdensity_99_07_eu_med = 0 if robot_ind==0
replace rdensity_95_swe	= 0 if robot_ind==0
**Define an Age-squared Variable**
gen age_sq=(age*age)
**Define a Global for Industry controls**
global ind_controls_full rdensity_95_swe 
*Change base level for muni1999 to Stockholm 0180*
fvset base 180 muni1999
*Save*
count
save "$projf/data/Main_analysis_data_full_v1.dta", replace
********************************************************************************

********************************************************************************
**Construct the working Sample for Panel Analysis - Switcher Analysis**
********************************************************************************
use "$projf/data/Linda_panel_robot230423.dta", clear
**Keep the Observations from 1999**
keep if year==1999
**Merge the LINDA data with the Educational data**
cap drop _merge
sort id
merge 1:1 id using "$projf\data\edu_data.dta" 
keep if _merge==3
**Merge the LINDA data with the 1993 characteristics**
cap drop _merge
sort id
merge 1:1 id using "$projf\data\characteristics_1993.dta" 
drop if _merge==2
**Merge the LINDA data with the Educational Transferability Scores**
cap drop _merge
sort college edumajor_3_93
merge m:1 college edumajor_3_93 using "$projf\data\transferability_index_edu3_93_230423.dta" 
drop if _merge==2
**Generate a dummy for individuals with low transferability of skills**
bys sni_2_1993: su hhi_id,d
bys sni_2_1993: egen med_hhi_id=median(hhi_id)
bys sni_2_1993: su HHI,d
bys sni_2_1993: egen med_HHI=median(HHI)
*Use the 50th percentile (within the Industry)
gen low_transfer=1 if (hhi_id >= med_hhi_id) & hhi_id !=.
*Use the 50th percentile (within the Industry)
replace low_transfer=0 if (hhi_id < med_hhi_id) & hhi_id !=.
**Descriptive stats**
tab low_transfer
**Define an Age-squared Variable**
gen age93 = age-6
gen age_sq93=(age93*age93)
gen nr_children93 = hh_children93
gen nr_adults93 = hh_size93-hh_children93
sum age93 nr_children93 nr_adults93, d
gen college93=0 if years_edu1993!=.
replace college93 = 1 if years_edu1993>12 & years_edu1993!=.
gen hschool93=0 if years_edu1993!=.
replace hschool93 = 1 if years_edu1993<=12 & years_edu1993>=10 & years_edu1993!=.
sum college93 hschool93
*Define an Indicator for Industry-Stayers based on sni2*
gen switcher_93_99=1 if (sni_2_99!=sni_2_1993) 
replace switcher_93_99=0 if (sni_2_99==sni_2_1993) 
replace switcher_93_99=. if (sni_2_1993==.|sni_2_99==.)
sum switcher_93_99
**Keep only individuals who are at least 18 years in 1993**
keep if age93>=18
*Change base level for muni1999 to Stockholm 0180*
fvset base 180 muni1999
*Save*
count
save "$projf/data/Main_analysis_data_switcher_v1.dta", replace
********************************************************************************

********************************************************************************
cap log close
********************************************************************************


**************************************************************************************************************************
****************************************** TABLES IN THE MAIN TEXT OF THE PAPER ******************************************
**************************************************************************************************************************

**Start log file**
log using "$logfiles/Table_regressions_v1.log", replace

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear

**Define a Global for Industry controls**
global ind_controls d_no_emp d_import_99_07_adj d_cap_stock_99_07 d_ict_cap_99_07 rdensity_95_swe med_EU_exp_adj lab_inten_99 d_profits_workers
*Define a Global for Household Controls*
global hh_controls dmarried_99_07 dcollege_99_07 dhschool_99_07 dnr_adults_99_07 dnr_children_99_07 i.muni1999


*********************************************************************************************
****************************************** TABLE 1 ******************************************
*********************************************************************************************

tab SNI2_s if pct_nw_rank_07!=.

use "$data/robot_data_all_updated190531.dta", clear
sort SNI2_s
by SNI2_s: sum no_workers_95_swe no_robots_swe rdensity_swe d_rdensity_99_07_swe if year==1999
by SNI2_s: sum no_workers_95_swe no_robots_swe rdensity_swe d_rdensity_99_07_swe if year==2007



*********************************************************************************************
****************************************** TABLE 2 ******************************************
*********************************************************************************************

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear

********************************************************************
** PANEL A - HOUSEHOLD DEMOGRAPHICS AND FINANCIAL CHARACTERISTICS **
********************************************************************
sum age male married college hschool nr_adults nr_children immigrant if pct_nw_rank_07!=.
sum hhtotalassets hhnetw hhfinw hhhouse hhdebt linc ln_earnings if pct_nw_rank_07!=.

*************************************
** PANEL B - VARIABLES OF INTEREST **
*************************************
sum d_rdensity_99_07_swe d_rdensity_99_07_eu_med if pct_nw_rank_07!=.

************************************
** PANEL C - DEPENDENT VARIABLES **
************************************
sum drisky_07 exit_stock_99_07 drisky_share_99_07 unemp_07 dln_earnings_99_07 lnw_07 pct_nw_rank_07 dnw_pct_99_07 dln_finw_99_07 wi_ratio_07 if pct_nw_rank_07!=.

****************************************
** PANEL D - INDUSTRY CHARACTERISTICS **
****************************************
sum d_no_emp d_import_99_07_adj d_cap_stock_99_07 d_ict_cap_99_07 rdensity_95_swe med_EU_exp_adj lab_inten_99 d_profits_workers if pct_nw_rank_07!=.



*********************************************************************************************
****************************************** TABLE 3 ******************************************
*********************************************************************************************

*****
** 1. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_dis_6_eu_med, title("NW rank")

*****
** 2. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_dis_6_eu_med, title("NW level")

*****
** 3. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_6_eu_med, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_dis_6_eu_med lnw_dis_6_eu_med dwealthr_6_eu_med using Table_3.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear


*********************************************************************************************
****************************************** TABLE 4 ******************************************
*********************************************************************************************

** Intersectoral Tranferability of Human Capital and Robots **

*Define a Global for Household Controls for Wealth Regressions*
global hh_controls_inter dmarried_99_07 dcollege_99_07 dhschool_99_07 dnr_adults_99_07 dnr_children_99_07 dlinc_99_07 

***************************
** PANEL A - FULL SAMPLE **
***************************

*****
** A1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
reg pct_nw_rank_07 low_transfer ind_low_transfer ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_dis_a1, title("NW rank")

reg pct_nw_rank_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_dis_a2, title("NW rank")


*****
** A3-4. LEVEL OF NET WEALTH (IHS) AS OF 2007
*****
reg lnw_07 low_transfer ind_low_transfer ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_ihs_a1, title("NW level")

reg lnw_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_ihs_a2, title("NW level")


*****
** A5-6. CHANGE IN THE RANK IN THE WEALTH DISTRIBUTION BETWEEN 1999-2007
*****
reg dnw_pct_99_07 low_transfer ind_low_transfer ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_chg_a1, title("change NW rank")

reg dnw_pct_99_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe*, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_chg_a2, title("change NW rank")



*******************************************
** PANEL B - HOUSEHOLDS ABOVE MEDIAN AGE **
*******************************************

sum age if pct_nw_rank_07!=., d

*****
** B1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
reg pct_nw_rank_07 low_transfer ind_low_transfer ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_dis_b1, title("NW rank")

reg pct_nw_rank_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_dis_b2, title("NW rank")


*****
** B3-4. LEVEL OF NET WEALTH (IHS) AS OF 2007
*****
reg lnw_07 low_transfer ind_low_transfer ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_ihs_b1, title("NW level")

reg lnw_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_ihs_b2, title("NW level")


*****
** B5-6. CHANGE IN THE RANK IN THE WEALTH DISTRIBUTION BETWEEN 1999-2007
*****
reg dnw_pct_99_07 low_transfer ind_low_transfer ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_chg_b1, title("change NW rank")

reg dnw_pct_99_07 low_transfer ind_low_transfer $hh_controls_inter i.muni1999 ind_fe* if age>38, cl(ind_edu)
estadd local Clustering "Low transfer-Industry"
estadd local Regional "Muni-Level"
estadd local Industry "Yes"
estimates store nw_chg_b2, title("change NW rank")


**Saving the Output**
cd $pathout
estout nw_dis_a1 nw_dis_a2 nw_ihs_a1 nw_ihs_a2 nw_chg_a1 nw_chg_a2 nw_dis_b1 nw_dis_b2 nw_ihs_b1 nw_ihs_b2 nw_chg_b1 nw_chg_b2 using Table_4.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*********************************************************************************************
****************************************** TABLE 5 ******************************************
*********************************************************************************************

*****
** 1. PERCENTAGE CHANGE IN THE EARNINGS BETWEEN 1999-2007
*****
**Base model**
ivreghdfe dln_earnings_99_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store earn_chg_6_eu_med, title("Earn_Chg")
**Base model + initial income and wealth controls**
ivreghdfe dln_earnings_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store earn_chg_9_eu_med, title("Earn_Chg")


*****
** 2. TRANSITION TO UNEMPLOYMENT BETWEEN 1999-2007 
*****
**Base model**
ivreghdfe unemp_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store unemp_6_eu_med, title("Unemp")
**Base model + initial income and wealth controls**
ivreghdfe unemp_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store unemp_9_eu_med, title("Unemp")


**Saving the Output**
cd $pathout
estout earn_chg_6_eu_med earn_chg_9_eu_med unemp_6_eu_med unemp_9_eu_med using Table_5.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*********************************************************************************************
****************************************** TABLE 6 ******************************************
*********************************************************************************************

*************************************
** PANEL A - FINANCIAL RISK TAKING **
*************************************

*****
** A1. PARTICIPATION STATUS AS OF 2007
*****
ivreghdfe drisky_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store status_4_eu_med, title("Parti")

*****
** A2. EXIT FROM THE STOCK MARKET BETWEEN 1999-2007
*****
ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_6_eu_med, title("Exit")

*****
** A3. CHANGE IN RISKY ASSETS SHARE BETWEEN 1999 - 2007
*****
ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_6_eu_med, title("Change")


*****************************************
** PANEL B - FINANCIAL WEALTH OUTCOMES **
*****************************************

*****
** B1. CHANGE IN FINANCIAL WEALTH
*****
ivreghdfe dln_finw_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_fw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) first 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dfinw_6_eu_med, title("D_FINW")

*****
** B2. NET-WEALTH to INCOME RATIO AS OF 2007
*****
ivreghdfe wi_ratio_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) first 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store winc_6_eu_med, title("W-Inc")


**Saving the Output**
cd $pathout
estout status_4_eu_med exit_6_eu_med drisky_6_eu_med dfinw_6_eu_med winc_6_eu_med using Table_6.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear


*********************************************************************************************
****************************************** TABLE 7 ******************************************
*********************************************************************************************

*********************************************************************
** PANEL A - WEALTH REGRESSIONS CONTROLLING FOR PAST INCOME GROWTH **
*********************************************************************

*****
** A1. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_dis_6a_eu_med, title("NW rank")

*****
** A2. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_dis_6a_eu_med, title("NW level")

*****
** A3. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_6a_eu_med, title("Change NW rank")


***********************************************************************
** PANEL B - WEALTH REGRESSIONS CONTROLLING FOR ACTIVE SAVINGS RATES **
***********************************************************************

*****
** B1. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank act_sav_rate_inc_00 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_dis_6b_eu_med, title("NW rank")

*****
** B2. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank act_sav_rate_inc_00 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_dis_6b_eu_med, title("NW level")

*****
** B3. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank act_sav_rate_inc_00 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_6b_eu_med, title("Change NW rank")



**Saving the Output**
cd $pathout
estout nw_dis_6a_eu_med lnw_dis_6a_eu_med dwealthr_6a_eu_med nw_dis_6b_eu_med lnw_dis_6b_eu_med dwealthr_6b_eu_med using Table_7.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear




*********************************************************************************************
****************************************** TABLE 8 ******************************************
*********************************************************************************************

*****
** A1. PERCENTAGE CHANGE IN THE EARNINGS BETWEEN 1999 and 2007
*****
ivreghdfe dln_earnings_99_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==1, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store earn_chg_low_edu, title("Earn_Chg")

ivreghdfe dln_earnings_99_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==0, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store earn_chg_high_edu, title("Earn_Chg")


*****
** A2. TRANSITION TO UNEMPLOYMENT BETWEEN 1999-2007 
*****
ivreghdfe unemp_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==1, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store unemp_low_edu, title("Unemp")

ivreghdfe unemp_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==0, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store unemp_high_edu, title("Unemp")


*****
** B1. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==1, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_low_edu, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==0, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_hig_edu, title("NW level")


*****
** B2. CHANGE IN NET WEALTH (LEVEL) BETWEEN 1999-2007
*****

ivreghdfe d_nw_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==1, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthlev_low_edu, title("D-wealthr")

ivreghdfe d_nw_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==0, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthlev_high_edu, title("D-wealthr")


*****
** C1. CHANGE IN RISKY ASSETS SHARE BETWEEN 1999 - 2007
*****
ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1 & low_edu==1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_low_edu, title("Change")

ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1 & low_edu==0, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_high_edu, title("Change")


*****
** C2. EXIT FROM THE STOCK MARKET BETWEEN 1999-2007
*****
ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==1, cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_low_edu, title("Exit")

ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if low_edu==0, cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_high_edu, title("Exit")


**Saving the Output**
cd $pathout
estout earn_chg_low_edu earn_chg_high_edu unemp_low_edu unemp_high_edu lnw_low_edu lnw_hig_edu dwealthlev_low_edu dwealthlev_high_edu drisky_low_edu drisky_high_edu exit_low_edu exit_high_edu using Table_8.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*********************************************************************************************
****************************************** TABLE 9 ******************************************
*********************************************************************************************

** Labor Income Process Estimation **
global pathoutinc "\\micro.intra\projekt\P0459$\P0459_gem\STATA\Third_project\20181108\output\first_diff\RFS_final\basecase\Table_9_Income"

foreach y in pre post {
foreach x in low med high {

********************************************************************************
* Upload Panel Data in Stata Format
********************************************************************************
** Upload the Panel Data ** 
use "$projf/data/incprocess_panel_final_RFS.dta", clear

********************************************************************************
* Filters for the Data 
********************************************************************************
** Focus on the sample of households above 20 and below 65 years of age **
keep if (age>20 & age<65)
** Focus on the right time period **
keep if `y'_period ==1
** Keep `x' Robot Exposure (based on 1993 or 1999 industry) **
keep if mean_`x'_rob_exp_`y'==1
** Exclude retirees, unemployed, and students from the sample **
keep if employed==1 
** Summary statistics **
sum broad_inc_hh logrealincome age, d
bys age: sum broad_inc_hh logrealincome age
sum retired unemployed student unknown_stat selfemployed
**Declare the data as Panel Data**
xtset pid year
** Save the analysis-ready data **
save "$projf/data/incprocess_panel_temp_RFS_analysis.dta", replace

******************************************************************************************************
* This program estimates the income profiles and decomposes the variance in permanent and transitory
*******************************************************************************************************
** Estimate the fixed effects regressions of real income on age dummies**
xtreg logrealincome i.age married if age>=21 & age<=64, i(pid) fe
estimates store output, title("Inc")
**Saving the Output**
cd $pathoutinc
estout output using Table_9_inc_estimation_`y'_1999_`x'.txt, cells(b(star fmt(5)) ///
t(par fmt(2))) collabels(none) ///
unstack stats(N r2, fmt(0 4 4) labels ///
("Observations" "R-squared")) /*nostar*/ style(fixed) replace label  
eststo clear
*then fit a third order polynomial to the age dummies to get the part of the deterministic profile
* Variance decomposition
predict e, resid
sort pid year
gen dif= e[_n+1]-e if pid==pid[_n+1] & year==year[_n+1]-1 & age>=21 & age<=64
replace dif= e[_n+2]-e if pid==pid[_n+2] & year==year[_n+2]-2 & age>=21 & age<=64
replace dif= e[_n+3]-e if pid==pid[_n+3] & year==year[_n+3]-3 & age>=21 & age<=64
replace dif= e[_n+4]-e if pid==pid[_n+4] & year==year[_n+4]-4 & age>=21 & age<=64
replace dif= e[_n+5]-e if pid==pid[_n+5] & year==year[_n+5]-5 & age>=21 & age<=64
replace dif= e[_n+6]-e if pid==pid[_n+6] & year==year[_n+6]-6 & age>=21 & age<=64
replace dif= e[_n+7]-e if pid==pid[_n+7] & year==year[_n+7]-7 & age>=21 & age<=64
replace dif= e[_n+8]-e if pid==pid[_n+8] & year==year[_n+8]-8 & age>=21 & age<=64
replace dif= e[_n+9]-e if pid==pid[_n+9] & year==year[_n+9]-9 & age>=21 & age<=64
replace dif= e[_n+10]-e if pid==pid[_n+10] & year==year[_n+10]-10 & age>=21 & age<=64
replace dif= e[_n+11]-e if pid==pid[_n+11] & year==year[_n+11]-11 & age>=21 & age<=64
replace dif= e[_n+12]-e if pid==pid[_n+12] & year==year[_n+12]-12 & age>=21 & age<=64
replace dif= e[_n+13]-e if pid==pid[_n+13] & year==year[_n+13]-13 & age>=21 & age<=64
replace dif= e[_n+14]-e if pid==pid[_n+14] & year==year[_n+14]-14 & age>=21 & age<=64
replace dif= e[_n+15]-e if pid==pid[_n+15] & year==year[_n+15]-15 & age>=21 & age<=64
***
gen     d= 1 if pid==pid[_n+1] & year==year[_n+1]-1 & age>=21 & age<=64
replace d= 2 if pid==pid[_n+2] & year==year[_n+2]-2 & age>=21 & age<=64
replace d= 3 if pid==pid[_n+3] & year==year[_n+3]-3 & age>=21 & age<=64
replace d= 4 if pid==pid[_n+4] & year==year[_n+4]-4 & age>=21 & age<=64
replace d= 5 if pid==pid[_n+5] & year==year[_n+5]-5 & age>=21 & age<=64
replace d= 6 if pid==pid[_n+6] & year==year[_n+6]-6 & age>=21 & age<=64
replace d= 7 if pid==pid[_n+7] & year==year[_n+7]-7 & age>=21 & age<=64
replace d= 8 if pid==pid[_n+8] & year==year[_n+8]-8 & age>=21 & age<=64
replace d= 9 if pid==pid[_n+9] & year==year[_n+9]-9 & age>=21 & age<=64
replace d= 10 if pid==pid[_n+10] & year==year[_n+10]-10 & age>=21 & age<=64
replace d= 11 if pid==pid[_n+11] & year==year[_n+11]-11 & age>=21 & age<=64
replace d= 12 if pid==pid[_n+12] & year==year[_n+12]-12 & age>=21 & age<=64
replace d= 13 if pid==pid[_n+13] & year==year[_n+13]-13 & age>=21 & age<=64
replace d= 14 if pid==pid[_n+14] & year==year[_n+14]-14 & age>=21 & age<=64
replace d= 15 if pid==pid[_n+15] & year==year[_n+15]-14 & age>=21 & age<=64

**Display the number of obs for each d
tab d
collapse (sd) dif if age>=21 & age<=64, by(d)
** Estimate the transitory and permanent shocks (all available years)**
gen dif2= dif*dif
*Restrict to a minimum value of d (where d=5 or d=10), if we think that for high values of d we have very few observations:
regress dif2 d if d<=5
estimates store sd_shocks_5, title("Shocks_5")
regress dif2 d if d<=10
estimates store sd_shocks_10, title("Shocks_10")
**Saving the Output**
cd $pathoutinc
estout sd_shocks_5 sd_shocks_10 using Table_9_sd_shocks_`y'_1999_all_`x'.txt, cells(b(star fmt(5)) ///
t(par fmt(2))) collabels(none) ///
unstack stats(N r2, fmt(0 4 4) labels ///
("Observations" "R-squared")) /*nostar*/ style(fixed) replace label  
eststo clear
** Clear the memory **
clear

********************************************************************************
* This program obtains predicted real income (raw and third order polynomial) 
********************************************************************************
use "$projf/data/incprocess_panel_temp_RFS_analysis.dta", replace
** Generate variable to store coefficients for age dummies **
gen coeff = .
** Age squared and cubed for polynomials **
gen age_sq_10  = age^2/10
gen age_cu_100 = age^3/100
** Estimate the fixed effects regressions of real income on age dummies**
xtreg logrealincome i.age married if age>=21 & age<=64, i(pid) fe
* Save fitted values
predict f_hat,xb
predict alpha_hat, u
gen 	exp_fitted = exp(f_hat + alpha_hat)
gen		exp_f_hat  = exp(f_hat)
** Save age dummy coefficients **
levelsof age if inrange(age,20,64), local(levels) 
foreach l of local levels {
qui sum exp_f_hat if age == `l'
qui replace coeff = r(mean) if age == `l'
}
** Get third-order polynomial coefficients **
reg coeff age age_sq_10 age_cu_100 if inrange(age,20,64)
estimates store age_coeffs`y'`x', title("Coeffs")

** Save the estimates **
reg coeff age age_sq_10 age_cu_100 if inrange(age,20,64)
scalar a_coeff = _b[_cons]
scalar b_coeff = _b[age]
scalar c_coeff = _b[age_sq_10]
scalar d_coeff = _b[age_cu_100]

*******************************************************************************
* Compute 3rd order polynomial (deterministic earnings profile)
*******************************************************************************
gen coeff_pol3 = a_coeff + b_coeff*age + c_coeff*age^2/10 + d_coeff*age^3/100 ///
							if inrange(age,20,64)
						
*******************************************************************************
* Make graphs to check results
*******************************************************************************
sort age
* Compare raw to polynomials 
line 	coeff       age     if inrange(age,24,63)   ///
|| line coeff_pol3 	age     if inrange(age,24,63),  ///
	ylabel(20(5)50) xlabel(24(5)63) name(inc_profile_`y'_1999_`x', replace)
cd $pathoutinc
graph export Table_9_inc_profile_`y'_1999_`x'.png, as(png) replace
**Export the data**
collapse (mean) coeff_pol3, by(age)
su coeff_pol3
cd $pathoutinc
export excel age coeff_pol3 using "Table_9_Determ_earnings_profile_`y'_1999_`x'", firstrow(variables) replace	
** Clear the memory **
clear

*********************************************************************************
* Retirement income
*********************************************************************************
** Upload the Panel Data ** 
use "$projf/data/incprocess_panel_final_RFS.dta", clear
*sum retired unemployed student unknown_stat selfemployed
** Keep retirees **
keep if age>=65 & retired==1
keep if `y'_period_longer==1
** Keep `x' Robot Exposure (based on 1999 industry) **
keep if mean_`x'_rob_exp_`y'==1
** Summary statistics **
sum broad_inc_hh logrealincome age, d
**Declare the data as Panel Data**
xtset pid year
**Rename the Variable**
ren logrealincome logrealbenefitinc
collapse (mean) logrealbenefitinc, by(age)
su logrealbenefitinc
cd $pathoutinc
export excel age logrealbenefitinc using "Table_9_retirement_inc_age_`y'_1999_`x'", firstrow(variables) replace
** Clear the memory **
clear

*********************************************************************************
* Unemployment income
*********************************************************************************
** Upload the Panel Data ** 
use "$projf/data/incprocess_panel_final_RFS.dta", clear
** Keep unemployed **
keep if age>=21 & age<=64 & unemployed==1
keep if `y'_period ==1
keep if mean_`x'_rob_exp_`y'==1
** Summary statistics **
sum broad_inc_hh logrealincome age, d
**Declare the data as Panel Data**
xtset pid year
**Rename the Variable**
ren logrealincome logrealunempincome
collapse (mean) logrealunempincome, by(age)
su logrealunempincome
cd $pathoutinc
export excel age logrealunempincome using "Table_9_unempl_inc_age_`y'_1999_`x'", firstrow(variables) replace
** Clear the memory **
clear

****************************************************
* Unemployment Probability
****************************************************
** Upload the Panel Data ** 
use "$projf/data/incprocess_panel_final_RFS.dta", clear
** Exclude retirees, students, and individuals with unknown status from the sample **
keep if (employed==1|unemployed==1)
sum retired unemployed employed student unknown_stat selfemployed
** Keep people age between 20 and 65 **
keep if age>=21 & age<=64 
keep if `y'_period_adj==1
** Keep `x' Robot Exposure (based on 1993 industry) **
keep if mean_`x'_rob_exp_`y'==1
** Summary statistics **
sum broad_inc_hh logrealincome age, d
**Declare the data as Panel Data**
xtset pid year
**Unemployment Probability**
su unemployed
**Rename the Variable**
collapse (mean) unemployed, by(age)
su unemployed
cd $pathoutinc
export excel age unemployed using "Table_9_unemployment_prob_age_`y'_1999_`x'", firstrow(variables) replace
** Clear the memory **
clear
*******************************************************************************************************

}
}

********************************************************************************
** PANEL A - LABOR INCOME PROCESS: COEFFICIENTS IN THE AGE POLYNOMIALS        **
********************************************************************************
**Saving the Output**
cd $pathout
estout age_coeffsprelow age_coeffspremed age_coeffsprehigh age_coeffspostlow age_coeffspostmed age_coeffsposthigh using Table_9_Panel_A.txt, cells(b(star fmt(5)) ///
t(par fmt(2))) collabels(none) ///
unstack stats(N r2, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear

clear


********************************************************************************
** PANEL B - VARIANCE DECOMPOSITIONS 									      **
********************************************************************************

** See the above code (use output files) **


********************************************************************************
** PANEL C - REPLACEMENT RATES AND UNEMPLOYMENT						          **
********************************************************************************

** See the above code (use output files) **


*********************************************************************************************
****************************************** TABLE 10 *****************************************
*********************************************************************************************

********************************************************************************
** Specifications 1-4** - Model Implied Moments
********************************************************************************

** See separate code **


********************************************************************************
** Specifications 5-8 - Empirical Counterparts 								  **
********************************************************************************
use "$projf/data/Main_analysis_data_v1.dta", clear
**Merge the LINDA data with the Robot Data**
sort SNI2_s 
cap drop _merge
merge m:1 SNI2_s using "$data/robot_data_2.dta" 
count
drop _merge

sum nwi_ratio_99 drisky_99 if drisky_07!=. & tercile_robot==1
sum nwi_ratio_99 drisky_99 if drisky_07!=. & tercile_robot==2
sum nwi_ratio_99 drisky_99 if drisky_07!=. & tercile_robot==3
sum nwi_ratio_99 drisky_99 if drisky_07!=. & (tercile_robot==1|tercile_robot==2|tercile_robot==3)

sum risky_share_99 if risky_share_99>0 & risky_share_99!=. & drisky_07!=. & tercile_robot ==1
sum risky_share_99 if risky_share_99>0 & risky_share_99!=. & drisky_07!=. & tercile_robot ==2
sum risky_share_99 if risky_share_99>0 & risky_share_99!=. & drisky_07!=. & tercile_robot ==3
sum risky_share_99 if risky_share_99>0 & risky_share_99!=. & drisky_07!=. & (tercile_robot==1|tercile_robot==2|tercile_robot==3)


clear

cap log close

*********************************************************************************************
****************************************** FIGURE 1 *****************************************
*********************************************************************************************

**Plot the age-income profiles in the pre- and post-periods **

** Use the income profiles from Table 9

**Rename/label variables**
ren Post_High H_RS
la var H_RS "H_RS"
ren Post_Medium M_RS
la var M_RS "M_RS"
ren Post_Low L_RS
la var L_RS "L_RS"
**Rename/label variables**
ren Pre_High H_NRS
la var H_NRS "H_NRS"
ren Pre_Medium M_NRS
la var M_NRS "M_NRS"
ren Pre_Low L_NRS
la var L_NRS "L_NRS"
*Label variable**
la var age "Age"
**Plot the age-income profiles in the pre period **
graph twoway line H_NRS age || line M_NRS age || line L_NRS age, xlabel(20[5]70) ytitle("Income (in 100s)") ylabel(1000[1000]5000) title("Pre-Robotization Period") name(pre, replace)
graph export "pre.png", as(png) replace


*********************************************************************************************
****************************************** FIGURE 2 *****************************************
*********************************************************************************************

**Plot the age-income profiles in the post period **
graph twoway line H_RS age || line M_RS age || line L_RS age, xlabel(20[5]70) ytitle("Income (in 100s)") ylabel(1000[1000]5000) title("Post-Robotization Period") name(post, replace)
graph export "post.png", as(png) replace




*******************************************************************************************************************
****************************************** TABLES IN THE ONLINE APPENDIX ******************************************
*******************************************************************************************************************

**Start log file**
log using "$logfiles/Table_regressions_OA_v1.log", replace

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear

**Define a Global for Industry controls**
global ind_controls d_no_emp d_import_99_07_adj d_cap_stock_99_07 d_ict_cap_99_07 rdensity_95_swe med_EU_exp_adj lab_inten_99 d_profits_workers
*Define a Global for Household Controls*
global hh_controls dmarried_99_07 dcollege_99_07 dhschool_99_07 dnr_adults_99_07 dnr_children_99_07 i.muni1999


*************************************************************************************************
****************************************** TABLE O.A.1 ******************************************
*************************************************************************************************

** See separate notes **


*************************************************************************************************
****************************************** TABLE O.A.2 ******************************************
*************************************************************************************************

*****
** 1. EXIT FROM THE STOCK MARKET BETWEEN 1999 - 2007
*****
ivregress 2sls exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) first


*****
** 2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivregress 2sls pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) first

clear


*************************************************************************************************
****************************************** TABLE O.A.3 ******************************************
*************************************************************************************************

******************************************************************************
* Upload Panel Data in Stata Format
******************************************************************************
** Upload the Panel Data ** 
use "$projf/data/incprocess_panel_final_RFS.dta", clear
count
******************************************************************************
* Filters for the Data 
******************************************************************************
** Focus on the time period between 1995 - 2007 **
keep if (year>=1995 & year<=2007)
** Focus on the sample of households who age between 20 and 65 **
keep if (age>=21 & age<=64)
** Keep Low AND High Robot Exposure (based on 1999 industry) **
** Low Robot Exposure (based on 1999 industry) **
gen low_rob_exp_99=1 if (med_robot==1) & (employed==1) & selfemployed==0 & age>=21 & age<=64
replace low_rob_exp_99=0 if (med_robot==2) & (employed==1) & selfemployed==0 & age>=21 & age<=64
gen mean_low_rob_exp_99=low_rob_exp_99
drop low_rob_exp_99
tab mean_low_rob_exp_99
keep if (mean_low_rob_exp_99==0|mean_low_rob_exp_99==1)
count
** Exclude retirees, unemployed, and students from the sample **
keep if (employed==1) 
** Summary statistics **
sum broad_inc_hh logrealincome age, d
sum retired unemployed student unknown_stat selfemployed
**Declare the data as Panel Data**
xtset pid year
**Create dummy for 1999 to 2007 period**
cap drop post_period
gen post_period = 0
replace post_period = 1 if year>=1999 & year<=2007
sum post_period
**Calculate the income growth for the pre- and post periods**
foreach var of varlist logrealincome {
	foreach y in 1995 1998 1999 2007 {
	gen `var'_`y'=`var' if year==`y'
	bys pid: egen mean_`var'_`y'=mean(`var'_`y')
	drop `var'_`y'
}
}
*Calculate the income growth in the pre- and post periods**
foreach var of varlist logrealincome {
gen dinc_growth_pre=mean_`var'_1998-mean_`var'_1995
gen dinc_growth_post=mean_`var'_2007-mean_`var'_1999
winsor dinc_growth_pre, gen(wdinc_growth_pre) p(0.01)
replace dinc_growth_pre=wdinc_growth_pre
drop wdinc_growth_pre
winsor dinc_growth_post, gen(wdinc_growth_post) p(0.01)
replace dinc_growth_post=wdinc_growth_post
drop wdinc_growth_post
}
**Keep one obs for pre- and post-period**
duplicates drop pid post_period, force 
** Declare as panel data*
xtset pid post_period
*Income growth variable*
gen inc_growth=dinc_growth_pre if post_period==0
replace inc_growth=dinc_growth_post if post_period==1
*Define a high robot industry*
gen high_robot=1 if mean_low_rob_exp_99==0
replace high_robot=0 if mean_low_rob_exp_99==1
*****Create balanced sample******
keep if mi(inc_growth)==0
by pid: gen periods = [_N]
tab periods
keep if periods==2
count
xtset pid post_period

******************************************************************************
**Run the diff-in-diff regressions**
******************************************************************************
**(Specification 1)**
areg inc_growth 1.post_period##1.high_robot, absorb(SNI2_s) cl(pid)
estadd local Clustering "HH"
estimates store incgrowth1, title("Inc Growth")
**(Specification 2)**
areg inc_growth 1.post_period##1.high_robot, absorb(SNI2_s) cl(sni_2)
estadd local Clustering "sni_2"
estimates store incgrowth2, title("Inc Growth")

******************************************************************************
**PANEL B: INCOME GROWTH REGRESSIONS**
******************************************************************************
cd $pathout
estout incgrowth1 incgrowth2 using Table_OA_3_panel_B.txt, cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label

******************************************************************************
* PANAL A: PARIWISE CORRELATIONS
******************************************************************************
*Income growth per industry*
bys SNI2_s: sum(dinc_growth_pre), d
bys SNI2_s: egen median_dinc_growth_pre=median(dinc_growth_pre)
bys SNI2_s: egen mean_dinc_growth_pre=mean(dinc_growth_pre)
bys SNI2_s: sum median_dinc_growth_pre mean_dinc_growth_pre

bys SNI2_s: sum(dinc_growth_post), d
bys SNI2_s: egen median_dinc_growth_post=median(dinc_growth_post)
bys SNI2_s: egen mean_dinc_growth_post=mean(dinc_growth_post)
bys SNI2_s: sum median_dinc_growth_post mean_dinc_growth_post

*Save just one observation per industry*
duplicates drop SNI2_s, force
count
cap drop _merge
replace year = 2007
sort SNI2_s year
keep SNI2_s mean_dinc_growth_pre mean_dinc_growth_post
save income_growth.dta, replace

*Merge data*
use "$data/change_in_employment.dta", clear
sort SNI2_s
save change_employment.dta, replace

use "$projf/data/Main_analysis_data_v1.dta", clear
duplicates drop SNI2_s, force
sort SNI2_s
merge 1:1 SNI2_s using change_employment.dta
erase change_employment.dta
drop _merge
merge 1:1 SNI2_s using income_growth.dta
erase income_growth.dta
drop _merge
count

**Label Variables**
la var d_rdensity_99_07_eu_med "Robotization (EU)"
la var d_import_99_07 "Chinese Import Exposure"
la var med_EU_exp "European Import Exposure"
la var d_cap_stock_99_07 "Change in Capital"
la var d_gross_profits_99_07 "Change in Profits"
la var d_ict_cap_99_07 "Change in ICT Capital"
la var lab_inten_99 "Labor Intensity"
la var d_no_emp "Change in Employment (93-98)"
la var d_no_emp_99_07 "Change in Employment (93-98)"
la var mean_dinc_growth_pre "Income Growth (93-98)"
la var mean_dinc_growth_post "Income Growth (99-07)"

*Output correlations**
cd $pathout
asdoc pwcorr d_rdensity_99_07_eu_med d_rdensity_99_07_swe d_no_emp d_no_emp_99_07 mean_dinc_growth_pre mean_dinc_growth_post d_import_99_07_adj med_EU_exp_adj d_cap_stock_99_07 d_ict_cap_99_07 lab_inten_99 d_gross_profits_99_07, save(Table_OA_3_panel_A.doc) replace label


*************************************************************************************************
****************************************** TABLE O.A.4 ******************************************
*************************************************************************************************

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear

** Educational Majors by Intersectoral Transferability **
use "$projf\data\transferability_index_edu3_230423.dta", clear
sort HHI
br

*************************************************************************************************
****************************************** TABLE O.A.5 ******************************************
*************************************************************************************************

** Industry Switching and Intersectoral Transferability of Educational Majors **

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear


*****************************************************************
** SWITCH INDUSTRY REGRESSIONS - PART 1 - SPECIFICATIONS 1-2   **
*****************************************************************
*Define a Global for Household Controls for Wealth Regressions*
global hh_controls_switch age age_sq male married college hschool nr_adults nr_children linc immigrant hhnetw_sav2 hhnetw_sav3 hhnetw_sav4 i.muni1999

*****
** 1. SWITCH INDUSTRY (sni2-level) BETWEEN 1999 AND 2006 (the industry classification changes in 2007)*
*****
reg switcher2_99_06 HHI $hh_controls_switch if drisky_07 !=. & sni_2_06!=., cl(sni_2)
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store switch1, title("Switch Ind")

*****
** 2. SWITCH INDUSTRY (sni2-level) BETWEEN 1999 AND 2006 (the industry classification changes in 2007)*
*****
reghdfe switcher2_99_06 HHI $hh_controls_switch if drisky_07 !=. & sni_2_06!=., cl(sni_2 muni1999) noabsorb
estadd local Clustering "Muni and Industry"
estadd local Regional FEs "Muni-Level"
estimates store switch2, title("Switch Ind")

*****************************************************************
** SWITCH INDUSTRY REGRESSIONS - PART 2 - SPECIFICATIONS 3-4   **
*****************************************************************

use "$projf/data/Main_analysis_data_switcher_v1.dta", clear

*Define a Global for Household Controls*
global hh_controls_switch2 age93 age_sq93 male married93 college93 hschool93 nr_adults93 nr_children93 linc93 immigrant i.muni1993

*****
** 3. OLS FOR SWITCH INDUSTRY*
*****
reg switcher_93_99 HHI $hh_controls_switch2, cl(sni_2_1993)
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store switch3, title("Switch Ind")

*****
** 4. OLS FOR SWITCH INDUSTRY*
*****
reghdfe switcher_93_99 HHI $hh_controls_switch2, noabsorb cl(muni1993 sni_2_1993)
estadd local Clustering "Muni and Industry"
estadd local Regional FEs "Muni-Level"
estimates store switch4, title("Switch Ind")

*****************************************************************
**Saving the Output**
*****************************************************************

cd $pathout
estout switch1 switch2 switch3 switch4 using Table_OA_5.txt, drop(*.muni*) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label

clear


*************************************************************************************************
****************************************** TABLE O.A.6 ******************************************
*************************************************************************************************

use "$projf/data/Main_analysis_data_v1.dta", clear

** Exposure to Robots and Household Net Wealth - Addressing Potential Sorting of Households **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_sorting_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_sorting_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_sorting_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_sorting_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_sorting_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if sni_2 == sni_2_1995, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_sorting_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_sorting_1 nw_sorting_2 lnw_sorting_1 lnw_sorting_2 dwealthr_sorting_1 dwealthr_sorting_2 using Table_OA_6.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 


*************************************************************************************************
****************************************** TABLE O.A.7 ******************************************
*************************************************************************************************

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_homeowner_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_homeowner_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_homeowner_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_homeowner_2, title("NW level")

*****
** 4-5. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_homeowner_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank homeow (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_homeowner_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_homeowner_1 nw_homeowner_2 lnw_homeowner_1 lnw_homeowner_2 dwealthr_homeowner_1 dwealthr_homeowner_2 using Table_OA_7.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear


*************************************************************************************************
****************************************** TABLE O.A.8 ******************************************
*************************************************************************************************

**Exposure to Robots and Household Net Wealth - Controlling for Risk Exposure**

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_riskyshare_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_riskyshare_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_riskyshare_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_riskyshare_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_riskyshare_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank risky_share (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_riskyshare_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_riskyshare_1 nw_riskyshare_2 lnw_riskyshare_1 lnw_riskyshare_2 dwealthr_riskyshare_1 dwealthr_riskyshare_2 using Table_OA_8.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 



*************************************************************************************************
****************************************** TABLE O.A.9 ******************************************
*************************************************************************************************

** Exposure to Robots and Changes in Household Debt **

*****
** 1. PERCENTAGE CHANGE IN DEBT OUTSTANDING BETWEEN 1999-2007
*****
ivregress 2sls dln_debt_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) first
estadd local Clustering "2-Digit-Industry"
estadd local Regional FEs "Muni-Level"
estimates store ddebt_3_eu_med, title("D-Debt")

ivreghdfe dln_debt_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) first 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store ddebt_6_eu_med, title("D-Debt")

**Saving the Output**
cd $pathout
estout ddebt_3_eu_med ddebt_6_eu_med using Table_OA_9.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*************************************************************************************************
****************************************** TABLE O.A.10 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Excluding Automotive Industry **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_auto_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_auto_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_auto_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_auto_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_auto_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=34 & sni_2!=35), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_auto_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_auto_1 nw_auto_2 lnw_auto_1 lnw_auto_2 dwealthr_auto_1 dwealthr_auto_2 using Table_OA_10.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 



*************************************************************************************************
****************************************** TABLE O.A.11 *****************************************
*************************************************************************************************

use "$projf/data/Main_analysis_data_full_v1.dta", clear

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_full_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_full_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_full_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_full_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_full_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls_full i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_full_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_full_1 nw_full_2 lnw_full_1 lnw_full_2 dwealthr_full_1 dwealthr_full_2 using Table_OA_11.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label




*************************************************************************************************
****************************************** TABLE O.A.12 *****************************************
*************************************************************************************************

use "$projf/data/Main_analysis_data_v1.dta", clear

** Exposure to Robots and Household Net Wealth - Excluding Rubber and Plastic Industry **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_plastic_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_plastic_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_plastic_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_plastic_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_plastic_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if (sni_2!=25 & sni_2!=26), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_plastic_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_plastic_1 nw_plastic_2 lnw_plastic_1 lnw_plastic_2 dwealthr_plastic_1 dwealthr_plastic_2 using Table_OA_12.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label



*************************************************************************************************
****************************************** TABLE O.A.13 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Excluding Commuter Households **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_commuter_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_commuter_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_commuter_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_commuter_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_commuter_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if commuter1999==0, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_commuter_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_commuter_1 nw_commuter_2 lnw_commuter_1 lnw_commuter_2 dwealthr_commuter_1 dwealthr_commuter_2 using Table_OA_13.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label

drop commuter1999



*************************************************************************************************
****************************************** TABLE O.A.14 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Verification of Statistical Inference **

*****
** 1-3. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(SNI2_s)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_verification_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_verification_2, title("NW rank")

boottest d_rdensity_99_07_swe, ar seed(1999998888) weighttype(normal)

*****
** 4-6. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(SNI2_s) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_verification_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_verification_2, title("NW level")

boottest d_rdensity_99_07_swe, ar seed(1999998888) weighttype(normal)

*****
** 7-9. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(SNI2_s)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_verification_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_verification_2, title("Change NW rank")

boottest d_rdensity_99_07_swe, ar seed(1999998888) weighttype(normal)

**Saving the Output**
cd $pathout
estout nw_verification_1 nw_verification_2 lnw_verification_1 lnw_verification_2 dwealthr_verification_1 dwealthr_verification_2 using Table_OA_14.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*************************************************************************************************
****************************************** TABLE O.A.15 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Using the 1999 Employment as the Base Value **

global ind_controls_99 d_no_emp d_import_99_07_adj d_cap_stock_99_07 d_ict_cap_99_07 rdensity_swe99 med_EU_exp_adj lab_inten_99 d_profits_workers


*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_99_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_99_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_99_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_99_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_99_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls_99 i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe99=d_rdensity_99_07_eu_med99), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_99_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_99_1 nw_99_2 lnw_99_1 lnw_99_2 dwealthr_99_1 dwealthr_99_2 using Table_OA_15.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label



*************************************************************************************************
****************************************** TABLE O.A.16******************************************
*************************************************************************************************

**Exposure to Robots and Household Net Wealth - Accounting for Life-Cycle Effects and Preference Shifters**

*************************************
*Generate some additional covariates*
gen age_hschool = age*hschool
gen age_sq_hschool = age_sq*hschool
gen age_college = age*college
gen age_sq_college = age_sq*college
gen age_male = age*male
gen age_sq_male = age_sq*male
*************************************
gen dhh_size = dnr_adults_99_07+dnr_children_99_07
*************************************
egen unemploybenefits_all = rowtotal(unemploybenefits1999-unemploybenefits2006)
gen unemp_all = (unemploybenefits_all>0)
*************************************
gen ln_disp_inc_95=ln(inchh_disp1995)
gen ln_disp_inc_97=ln(inchh_disp1997)
gen ln_disp_inc_99=ln(inchh)
gen d_inc_99_97 = ln_disp_inc_99-ln_disp_inc_97
gen d_inc_97_95 = ln_disp_inc_97-ln_disp_inc_95
winsor d_inc_99_97 if mi(d_inc_99_97)==0, gen(wd_inc_99_97) p(.01)
winsor d_inc_97_95 if mi(d_inc_97_95)==0, gen(wd_inc_97_95) p(.01)  
*************************************
gen selfemployed_99 = (enterpr_inc_hh1999>0)
gen selfemployed_07 = (enterpr_inc_hh2007>0)
gen earnings_99 = (ln_earnings_99>0)
gen earnings_07 = (ln_earnings_07>0)
*************************************
sum age age_sq hschool college male age_hschool age_sq_hschool age_college age_sq_college age_male age_sq_male if drisky_07 !=.  
sum married nr_children hh_size dhh_size unemp_all ln_disp_inc_95 wd_inc_99_97 wd_inc_97_95 homeow_07 homeow_99 selfemployed_99 selfemployed_07 earnings_99 earnings_07 if drisky_07 !=.
*************************************
global hh_controls_bn age age_sq hschool college male age_hschool age_sq_hschool age_college age_sq_college age_male age_sq_male married nr_children hh_size dhh_size unemp_all ln_disp_inc_95 wd_inc_99_97 wd_inc_97_95 homeow_99 homeow_07 selfemployed_99 selfemployed_07 earnings_99 earnings_07 i.muni1999 
*************************************


*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_bn_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_bn_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_bn_1, title("NW level")

ivreghdfe lnw_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_bn_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_bn_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls_bn $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_bn_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_bn_1 nw_bn_2 lnw_bn_1 lnw_bn_2 dwealthr_bn_1 dwealthr_bn_2 using Table_OA_16.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 

drop age_hschool age_sq_hschool age_college age_sq_college age_male age_sq_male dhh_size unemploybenefits_all unemp_all ln_disp_inc_95 ln_disp_inc_97 ln_disp_inc_99 d_inc_99_97 d_inc_97_95 selfemployed_99 selfemployed_07 earnings_99 earnings_07



*************************************************************************************************
****************************************** TABLE O.A.17******************************************
*************************************************************************************************

** Exposure to Robots and Income Growth - Including Transfers **

*****
** 1-2. CHANGE IN DISPOSABLE INCOME BETWEEN 1999-2007
*****
ivreghdfe dln_disp_nocap_99_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dispinc_1, title("Entry")

ivreghdfe dln_disp_nocap_99_07 $hh_controls $ind_controls (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dispinc_2, title("Entry")


**Saving the Output**
cd $pathout
estout dispinc_1 dispinc_2 using Table_OA_17.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 

drop dln_disp_nocap_99_07



*************************************************************************************************
****************************************** TABLE O.A.18******************************************
*************************************************************************************************

** Exposure to Robots and Stock Market Entry **

*****
** 1-2. ENTRY IN THE STOCK MARKET BETWEEN 1999-2007
*****
ivreghdfe entry_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store entry_1, title("Entry")

ivreghdfe entry_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store entry_2, title("Entry")


**Saving the Output**
cd $pathout
estout entry_1 entry_2 using Table_OA_18.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 

drop entry_stock_99_07



*************************************************************************************************
****************************************** TABLE O.A.19 *****************************************
*************************************************************************************************

**Exposure to Robots and Housing Investments**

*****
** 1-2. BEING HOMEOWNER IN 2007
*****
ivregress 2sls homeow_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) first
estadd local Clustering "2-Digit-Industry"
estadd local Regional FEs "Muni-Level"
estimates store howner_1, title("Entry")

ivreghdfe homeow_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store howner_2, title("Entry")

*****
** 3-4. BUYING AN HOUSE BETWEEN 1999 - 2007
*****
ivregress 2sls buy_house_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "2-Digit-Industry"
estadd local Regional FEs "Muni-Level"
estimates store buyh_1, title("Entry")

ivreghdfe buy_house_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store buyh_2, title("Entry")

*****
** 5-6. SELLING A HOUSE BETWEEN 1999 - 2007
*****
ivregress 2sls sell_house_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) first
estadd local Clustering "2-Digit-Industry"
estadd local Regional FEs "Muni-Level"
estimates store sellh_1, title("Exit")

ivreghdfe sell_house_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store sellh_2, title("Exit")


**Saving the Output**
cd $pathout
estout howner_1 howner_2 buyh_1 buyh_2 sellh_1 sellh_2 using Table_OA_19.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label

drop buy_house_99_07 sell_house_99_07



*************************************************************************************************
****************************************** TABLE O.A.20 *****************************************
*************************************************************************************************

**Exposure to Robots and Financial Risk Taking Behavior - Controlling for Income Growth Expectations**

*************************************
** PANEL A - FINANCIAL RISK TAKING **
*************************************

*****
** A1. PARTICIPATION STATUS AS OF 2007
*****
ivreghdfe drisky_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store status_incgrowth_1a, title("Parti")

ivreghdfe drisky_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store status_incgrowth_2a, title("Parti")

*****
** A2. EXIT FROM THE STOCK MARKET BETWEEN 1999-2007
*****
ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_incgrowth_1a, title("Exit")

ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_incgrowth_2a, title("Exit")

*****
** A3. CHANGE IN RISKY ASSETS SHARE BETWEEN 1999 - 2007
*****
ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1, cl(sni_2)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_incgrowth_1a, title("Change")

ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_99_07 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_incgrowth_2a, title("Change")


*************************************
** PANEL B - FINANCIAL RISK TAKING **
*************************************

*****
** B1. PARTICIPATION STATUS AS OF 2007
*****
ivreghdfe drisky_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store status_incgrowth_1b, title("Parti")

ivreghdfe drisky_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store status_incgrowth_2b, title("Parti")

*****
** B2. EXIT FROM THE STOCK MARKET BETWEEN 1999-2007
*****
ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_incgrowth_1b, title("Exit")

ivreghdfe exit_stock_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store exit_incgrowth_2b, title("Exit")

*****
** B3. CHANGE IN RISKY ASSETS SHARE BETWEEN 1999 - 2007
*****
ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1, cl(sni_2)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_incgrowth_1b, title("Change")

ivreghdfe drisky_share_99_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank dln_earnings_95_98 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if drisky_99==1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store drisky_incgrowth_2b, title("Change")


**Saving the Output**
cd $pathout
estout status_incgrowth_1a status_incgrowth_2a exit_incgrowth_1a exit_incgrowth_2a drisky_incgrowth_1a drisky_incgrowth_2a status_incgrowth_1b status_incgrowth_2b exit_incgrowth_1b ///
exit_incgrowth_2b drisky_incgrowth_1b drisky_incgrowth_2b using Table_OA_20.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label  
eststo clear



*************************************************************************************************
****************************************** TABLE O.A.21 *****************************************
*************************************************************************************************

**Exposure to Robots and Household Net Wealth - Controlling for Past Income Growth and Average Income**

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_incgrowth_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_incgrowth_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_incgrowth_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_incgrowth_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_incgrowth_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls dln_earnings_95_98 linc_perm i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_incgrowth_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_incgrowth_1 nw_incgrowth_2 lnw_incgrowth_1 lnw_incgrowth_2 dwealthr_incgrowth_1 dwealthr_incgrowth_2 using Table_OA_21.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 


drop dln_earnings_95_98 linc_perm



*************************************************************************************************
****************************************** TABLE O.A.22 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Excluding Displaced Workers **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_displaced_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_displaced_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_displaced_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_displaced_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_displaced_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med) if unemp_07!=1, cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_displaced_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_displaced_1 nw_displaced_2 lnw_displaced_1 lnw_displaced_2 dwealthr_displaced_1 dwealthr_displaced_2 using Table_OA_22.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label



*************************************************************************************************
****************************************** TABLE O.A.23 *****************************************
*************************************************************************************************

** Exposure to Robots and Household Net Wealth - Controlling for Initial Total Savings Rate **

*****
** 1-2. RANK IN THE WEALTH DISTRIBUTION AS OF 2007
*****
ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store nw_totsav_1, title("NW rank")

ivreghdfe pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store nw_totsav_2, title("NW rank")

*****
** 3-4. IHS NET WEALTH AS OF 2007
*****
ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2) 
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store lnw_totsav_1, title("NW level")

ivreghdfe lnw_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999) 
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store lnw_totsav_2, title("NW level")

*****
** 5-6. CHANGE IN NET WEALTH RANKS BETWEEN 1999-2007
*****
ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_totsav_1, title("Change NW rank")

ivreghdfe dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank tot_sav_rate_inc_99 (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store dwealthr_totsav_2, title("Change NW rank")


**Saving the Output**
cd $pathout
estout nw_totsav_1 nw_totsav_2 lnw_totsav_1 lnw_totsav_2 dwealthr_totsav_1 dwealthr_totsav_2 using Table_OA_23.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label


*************************************************************************************************
****************************************** TABLE O.A.24******************************************
*************************************************************************************************

** Exposure to Robots and Saving Rates (2000-2007) **

*****
** 1-2. 
*****
ivreghdfe mean_act_sav_rate_00_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2)  
estadd local Clustering "sni_2"
estadd local Regional FEs "Muni-Level"
estimates store savings_1, title("Savings")

ivreghdfe mean_act_sav_rate_00_07 $hh_controls $ind_controls i.dec_nw_rank i.dec_earn_rank (d_rdensity_99_07_swe=d_rdensity_99_07_eu_med), cl(sni_2 muni1999)  
estadd local Clustering "sni_2 muni"
estadd local Regional FEs "Muni-Level"
estimates store savings_2, title("Savings")


**Saving the Output**
cd $pathout
estout savings_1 savings_2 using Table_OA_24.txt, drop(*.muni1999) cells(b(star fmt(5)) ///
se(par fmt(4))) collabels(none) ///
unstack stats(N r2 Clustering, fmt(0 4 4) labels ///
("Observations" "R-squared" "Clustering")) starlevels(* 0.10 ** 0.05  *** 0.01)  style(fixed) replace label 


drop mean_act_sav_rate_00_07



*************************************************************************************************
****************************************** TABLE O.A.25******************************************
*************************************************************************************************

** Quantifying the relative importance of alternative mechanisms **

***Specification #1: To Test the Contribution of Change in Earnings in 2000 - 2007 ***
ivmediate pct_nw_rank_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank, mediator(dln_earnings_99_07) treatment(d_rdensity_99_07_swe) instrument(d_rdensity_99_07_eu_med) vce(cluster sni_2) full

***Specification #2: To Test the Contribution of Change in Earnings in 2000 - 2007 ***
ivmediate dnw_pct_99_07 $hh_controls $ind_controls i.dec_earn_rank i.dec_nw_rank, mediator(dln_earnings_99_07) treatment(d_rdensity_99_07_swe) instrument(d_rdensity_99_07_eu_med) vce(cluster sni_2) full

** Close the log file **
cap log close

*************************************************************************************************
****************************************** TABLE O.A.26******************************************
*************************************************************************************************

** Use the income profiles from Table 9 **

**Rename/label variables**
ren Post_High H_RS
la var H_RS "H_RS"
ren Post_Medium M_RS
la var M_RS "M_RS"
ren Post_Low L_RS
la var L_RS "L_RS"
**Rename/label variables**
ren Pre_High H_NRS
la var H_NRS "H_NRS"
ren Pre_Medium M_NRS
la var M_NRS "M_NRS"
ren Pre_Low L_NRS
la var L_NRS "L_NRS"
** Income difference between post- and pre-periods**
gen diff_high=(H_RS-H_NRS)
la var diff_high "(H_RS-H_NRS)"
gen diff_med=(M_RS-M_NRS)
la var diff_med "(M_RS-M_NRS)"
gen diff_low=(L_RS-L_NRS)
la var diff_low "(L_RS-L_NRS)"
drop H- K
*Label variable**
la var age "Age"

**Panel A - Income Estimates in Levels**
tabstat H_RS M_RS L_RS H_NRS M_NRS L_NRS , stats(mean p50 sd N) columns(stats)

** Panel B - Differences between Post- and Pre-Periods**
tabstat diff_*, stats(mean p50 sd N) columns(stats) save


*************************************************************************************************
****************************************** FIGURE O.A.1 *****************************************
*************************************************************************************************

**Start log file**
log using "$logfiles/Figures_OA_v1.log", replace

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear

cd "$pathout/figures/"
keep if drisky_07 !=. /*NOTE: to keep the orginal sample*/

**Kernel Density Functions - treatment vs. control**

*Net Wealth in 1999
sum hhnetw, d
gen hhnetw_p99 = r(p99)
gen hhnetw_p1 = r(p1)
kdensity hhnetw if low_transfer == 1 & year==1999 & hhnetw<hhnetw_p99 & hhnetw>hhnetw_p1, addplot(kdensity hhnetw if low_transfer == 0 & year==1999 & hhnetw<hhnetw_p99 & hhnetw>hhnetw_p1) ///
	title(" ") xtitle("Net Wealth in 1999") lpattern(dash solid) legend(ring(2) label(1 "Low-Skill Transferability") label(2 "High-Skill Transferability")) 
graph export nw_did.png, as(png) replace


*************************************************************************************************
****************************************** FIGURE O.A.2 *****************************************
*************************************************************************************************

*Income in 1999
sum inchh, d
gen inchh_p99 = r(p99)
kdensity inchh if low_transfer == 1 & year==1999 & inchh<inchh_p99, addplot(kdensity inchh if low_transfer == 0 & year==1999 & inchh<inchh_p99) ///
	title(" ") xtitle("Income in 1999") lpattern(dash solid) legend(ring(2) label(1 "Low-Skill Transferability") label(2 "High-Skill Transferability")) 
graph export inc_did.png, as(png) replace


*************************************************************************************************
****************************************** FIGURE O.A.3 (a) *************************************
*************************************************************************************************

**Household Controls**
global hh_controls_pl dmarried_99_07 dcollege_99_07 dhschool_99_07 dnr_adults_99_07 dnr_children_99_07 d_no_emp d_import_99_07_adj d_cap_stock_99_07 d_ict_cap_99_07 rdensity_95_swe med_EU_exp_adj lab_inten_99 d_profits_workers

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear
sort SNI2_s 

** RUN REGRESSIONS
label var pct_nw_rank_07 "Wealth_Rank" 

tempname memhold
tempfile results
postfile `memhold' t_stat using "`results'"
forvalues i=1/1000 { 

preserve
// Keep only one industry observation + associated robot change
bysort SNI2_s: keep if _n ==1
keep SNI2_s d_rdensity_99_07_swe d_rdensity_99_07_eu_med

// Shuffle robot variable across industries
local seed = 7111 + `i'
set seed `seed'
shufflevar d_rdensity_99_07_swe 
shufflevar d_rdensity_99_07_eu_med
drop d_rdensity_99_07_swe
drop d_rdensity_99_07_eu_med

// Create temporary file of shuffled robot change
tempfile temp
save `temp'
restore

// Add shuffled robot change variable level back to main sample
capture drop d_rdensity_99_07_swe_shuffled d_rdensity_99_07_eu_med_shuffled
merge m:1 SNI2_s using `temp', nogen

/***************************** MAIN SETTINGS **********************************/
global stand_errors 	"sni_2 muni1999"
global dep_variable		pct_nw_rank_07
global expl_variable	d_rdensity_99_07_swe_shuffled
global controls			$hh_controls_pl
global fe				"a(muni1999 dec_nw_rank dec_earn_rank)"
/******************************************************************************/

estimates clear
	
	ivreghdfe ${dep_variable} 					/// DEPENDENT
	${controls}									/// HOUSEHOLD CONTROLS
	(${expl_variable}=d_rdensity_99_07_eu_med_shuffled), /// INSTRUMENTED VARIABLE
	${fe} cl($stand_errors) 					// 
	estimates store m1

// Post t-stat to results file
post `memhold' (_b[d_rdensity_99_07_swe_shuffled]/_se[d_rdensity_99_07_swe_shuffled])

} 

// Finish up results file
postclose `memhold'
use "`results'", clear
sum t_stat
save $pathout/T_stats_randomized_WR_2007, replace 

// Plot distribution of t-stats
histogram t_stat, frequency normal xtitle("t-Statistic") xlabel(-4(1)4) xscale(r(-4 4))
graph export $pathout/hist_random_WR_2007.pdf, replace 



*************************************************************************************************
****************************************** FIGURE O.A.3 (b) *************************************
*************************************************************************************************

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear
sort SNI2_s 

** RUN REGRESSIONS
label var lnw_07 "IHS Wealth" 

tempname memhold
tempfile results
postfile `memhold' t_stat using "`results'"
forvalues i=1/1000 { 

preserve
// Keep only one industry observation + associated robot change
bysort SNI2_s: keep if _n ==1
keep SNI2_s d_rdensity_99_07_swe d_rdensity_99_07_eu_med


// Shuffle robot variable across industries
local seed = 7111 + `i'
set seed `seed'
shufflevar d_rdensity_99_07_swe 
shufflevar d_rdensity_99_07_eu_med
drop d_rdensity_99_07_swe
drop d_rdensity_99_07_eu_med

// Create temporary file of shuffled robot change
tempfile temp
save `temp'
restore

// Add shuffled robot change variable level back to main sample
capture drop d_rdensity_99_07_swe_shuffled d_rdensity_99_07_eu_med_shuffled
merge m:1 SNI2_s using `temp', nogen

/***************************** MAIN SETTINGS **********************************/
global stand_errors 	"sni_2 muni1999"
global dep_variable		lnw_07
global expl_variable	d_rdensity_99_07_swe_shuffled
global controls			$hh_controls_pl
global fe				"a(muni1999 dec_nw_rank dec_earn_rank)"
/******************************************************************************/

estimates clear
	
	ivreghdfe ${dep_variable} 					/// DEPENDENT
	${controls}									/// HOUSEHOLD CONTROLS
	(${expl_variable}=d_rdensity_99_07_eu_med_shuffled), /// INSTRUMENTED VARIABLE
	${fe} cl($stand_errors) 					// 
	estimates store m1


// Post t-stat to results file
post `memhold' (_b[d_rdensity_99_07_swe_shuffled]/_se[d_rdensity_99_07_swe_shuffled])

} 

// Finish up results file
postclose `memhold'
use "`results'", clear
sum t_stat
save $pathout/T_stats_randomized_IHS_2007, replace 

// Plot distribution of t-stats
histogram t_stat, frequency normal xtitle("t-Statistic") xlabel(-4(1)4) xscale(r(-4 4))
graph export $pathout/hist_random_IHS_2007.pdf, replace 



*************************************************************************************************
****************************************** FIGURE O.A.3 (c) *************************************
*************************************************************************************************

**Upload analysis dataset**
use "$projf/data/Main_analysis_data_v1.dta", clear
sort SNI2_s 

** RUN REGRESSIONS
label var dnw_pct_99_07 "change NW rank" 

tempname memhold
tempfile results
postfile `memhold' t_stat using "`results'"
forvalues i=1/1000 { 

preserve
// Keep only one industry observation + associated robot change
bysort SNI2_s: keep if _n ==1
keep SNI2_s d_rdensity_99_07_swe d_rdensity_99_07_eu_med

// Shuffle robot variable across industries
local seed = 7111 + `i'
set seed `seed'
shufflevar d_rdensity_99_07_swe 
shufflevar d_rdensity_99_07_eu_med
drop d_rdensity_99_07_swe
drop d_rdensity_99_07_eu_med

// Create temporary file of shuffled robot change
tempfile temp
save `temp'
restore

// Add shuffled robot change variable level back to main sample
capture drop d_rdensity_99_07_swe_shuffled d_rdensity_99_07_eu_med_shuffled
merge m:1 SNI2_s using `temp', nogen

/***************************** MAIN SETTINGS **********************************/
global stand_errors 	"sni_2 muni1999"
global dep_variable		dnw_pct_99_07
global expl_variable	d_rdensity_99_07_swe_shuffled
global controls			$hh_controls_pl
global fe				"a(muni1999 dec_nw_rank dec_earn_rank)"
/******************************************************************************/

estimates clear
	
	ivreghdfe ${dep_variable} 					/// DEPENDENT
	${controls}									/// HOUSEHOLD CONTROLS
	(${expl_variable}=d_rdensity_99_07_eu_med_shuffled), /// INSTRUMENTED VARIABLE
	${fe} cl($stand_errors) 					// 
	estimates store m1


// Post t-stat to results file
post `memhold' (_b[d_rdensity_99_07_swe_shuffled]/_se[d_rdensity_99_07_swe_shuffled])

} 

// Finish up results file
postclose `memhold'
use "`results'", clear
sum t_stat
save $pathout/T_stats_randomized_d_NWrank_99_07, replace 

// Plot distribution of t-stats
histogram t_stat, frequency normal xtitle("t-Statistic") xlabel(-4(1)4) xscale(r(-4 4))
graph export $pathout/hist_random_d_NWrank_99_07.pdf, replace 



*********************************************************************************************
**Close the log file
*********************************************************************************************
cap log close


*********************************************************************************************
**************************************** END OF CODE ****************************************
*********************************************************************************************



